using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TreeSharp;

using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.BehaviorTree;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

namespace FelMaster.Classes.Shaman
{
    class Enhance : TalentBase
    {
        public override string Name
        {
            get { return "Enhancement Shaman V0.1"; }
        }

        public override string KeySpell
        {
            get { return "Lava Lash"; }
        }
        
        private static bool ChkTotem(string totem)
        {
            bool totchk = false;
            foreach (Styx.WoWInternals.WoWObjects.WoWTotemInfo ti in Me.Totems)
            {
                if (ti.Name == totem)
                    totchk = true;
            }
            return totchk;
        }
        
        public List<WoWUnit> AOECHK(WoWPoint chkloc, double radius)
        {
            List<WoWUnit> hostile = ObjectManager.GetObjectsOfType<WoWUnit>(true, false);
            List<WoWUnit> avoid = new List<WoWUnit>();
            var MaxDistance2 = radius * radius;
            try
            {
                hostile = hostile.Where(x =>
                    x.IsAlive &&
                    (x.IsTargetingMeOrPet || x.IsTargetingMyPartyMember || x.IsTargetingMyRaidMember || x.IsTargetingPet) &&
                    !x.IsCritter &&
                    !x.IsPet &&
                    x.Location.Distance2DSqr(chkloc) < MaxDistance2
                ).ToList();
                avoid = hostile.Where(x =>
                    //check for controlled units, like sheep etc
                SimCraftBase.Instance.UnitIsControlled(x, true)
                ).ToList();
                if (avoid != null && avoid.Count > 0)
                {
                    hostile = null;
                    hostile.Add(Me.CurrentTarget);
                }
            }
            catch { }
            return hostile;
        }
        
        public override Composite SingleRotation
        {
            get {
                return new PrioritySelector(
                            SC.CastSpell("Searing Totem", a => !ChkTotem("Searing Totem"), "Searing Totem"),
                            SC.CastSpell("Stormstrike", a => true, "Stormstrike"),
                            SC.CastSpell("Lava Lash", a => true, "Lava Lash"),
                            SC.CastSpell("Chain Lightning", a => Me.GotTarget && AOECHK(Me.CurrentTarget.Location, 8).Count >= 2 && SC.PlayerCountBuff("Maelstrom Weapon") == 5, "Chain Lightning"),
                            SC.CastSpell("Lightning Bolt", a => SC.PlayerCountBuff("Maelstrom Weapon") == 5, "Lightning Bolt"),
                            SC.CastSpell("Unleash Elements", a => true, "Unleash Elements"),
                            SC.CastSpell("Lava Burst", a => SC.SpellCooldown("Flame Shock") > 2 && SC.TargetDebuffTimeLeft("Flame Shock") > 2 && SC.PlayerBuffTimeLeft("Unleash Flame") > 2, "Lava Burst"),
                            SC.CastSpell("Flame Shock", a => SC.PlayerHasBuff("Unleash Flame") || !SC.TargetHasDebuff("Flame Shock"), "Flame Shock"),
                            SC.CastSpell("Earth Shock", a => true, "Earth Shock"),
                            SC.CastSpell("Feral Spirit", a => true, "Feral Spirit"),
                            SC.CastSpell("Earth Elemental Totem", a => true, "Earth Elemental Totem"),
                            SC.CastSpell("Fire Nova", a => Me.GotTarget && AOECHK(Me.CurrentTarget.Location, 8).Count >= 2, "Fire Nova"),
                            SC.CastSpell("Spiritwalker's Grace", a => Me.IsMoving, "Spiritwalker's Grace"),
                            SC.CastSpell("Fire Nova", a => true, "Fire Nova")
                );
            }
        }

        public override Composite Buffs
        {
            get
            {
                return new PrioritySelector(
                    SC.CastBuff("Lightning Shield", a => true, "Lightning Shield")
                );
            }
        }

        public override Composite PVPRotation
        {
            get { throw new NotImplementedException(); }
        }

        public override Composite PVERotation
        {
            get
            {
                return SingleRotation;
            }
        }
    }
}